#include <testssisensoruni/TestSSISensorUniE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

TestSSISensorUni::EDROOMTopContext::EDROOMTopContext (TestSSISensorUni &act, TEDROOMInt32 & EDROOMpVarVMessageReceived ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,SsiCtrl(EDROOMcomponent.SsiCtrl)
	,StoreAndNextFrame(EDROOMcomponent.StoreAndNextFrame)
	, VMessageReceived ( EDROOMpVarVMessageReceived )
 {

}

TestSSISensorUni::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,SsiCtrl(contex.SsiCtrl)
	,StoreAndNextFrame(contex.StoreAndNextFrame)
	, VMessageReceived ( contex.VMessageReceived )
 {

}

	// EDROOMSearchContextTrans********************************************

bool TestSSISensorUni::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	TestSSISensorUni::EDROOMTopContext::FCheckFiles()

{
	FILE *originFile,*finalFile;
	bool_t ok=true;
	byte_t originBuffer[50], finalBuffer[50];
 
   finalFile=fopen("FUpFrames.jpg","rb");
   originFile=fopen("OUpFrames.jpg","rb");
   do
	{
	   for (TEDROOMInt32 i =0; i< 50; i++)
	   {
		   originBuffer[i]=0;
		   finalBuffer[i]=0;
	   }
	   fread(originBuffer, 1,1, originFile);
	   fread(finalBuffer, 1,1, finalFile);
	   if(originBuffer[0]!=finalBuffer[0])
	   {
	   	ok=false;
	   }
	} while (!feof(originFile) && !feof(finalFile));
   fclose(originFile);
   fclose(finalFile);
   if (ok)
   {
   	printf("TEST SSI-SENSOR UNIDIRECTIONAL: OK\n");
   }
   else
   {
   	printf("TEST SSI-SENSOR UNIDIRECTIONAL: ERROR\n");
   }
}
 



void	TestSSISensorUni::EDROOMTopContext::FInit()

{
 
	init_CRC_word16_table();
	// send new Msg
	SsiCtrl.send(SToListening, EDROOMprioNormal);
 
}



void	TestSSISensorUni::EDROOMTopContext::FListening()

{
	//printf("TEST: Listening\n");
}



void	TestSSISensorUni::EDROOMTopContext::FStoreFrame()

{
 
	CDFrame & varCDFrame = *(CDFrame *)Msg->data; 
	FILE *fich;
	byte_t data[32];
	TEDROOMInt32 length;
 
   fich=fopen("FUpFrames.jpg","ab");
   ssi_link_layer_get_data(varCDFrame.dataFrame,data);
   length = ssi_link_layer_get_data_length(varCDFrame.dataFrame);
   if (fich==NULL)
   {
	   //printf("Error al intentar abrir el archivo final.jpg\n");
   }
   else
   {				
		fwrite(data, 1,length, fich);
		fclose(fich);
	}

   if (VMessageReceived==1789)
   {
	   FCheckFiles();
	   exit(0);
   }
   VMessageReceived++;
 
}



	//*****Pools



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

TestSSISensorUni::EDROOMTopState::EDROOMTopState (TestSSISensorUni &act ):
	  EDROOMTopContext( act , VMessageReceived )
	, VMessageReceived ( 1 )
{

}

	// EDROOMBehaviour**********************************************

void TestSSISensorUni::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				FInit();
				edroomNextState = Listening;
				break;
			case(StoreFrame):
				FStoreFrame();
				edroomNextState = Listening;
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Listening):
				FListening();
				edroomCurrentTrans=EDROOMListeningllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void TestSSISensorUni::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId TestSSISensorUni::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Listening

	// ***********************************************************************



TEDROOMTransId TestSSISensorUni::EDROOMTopState::EDROOMListeningllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (SDataFrame): 

				 if ( *Msg->GetPInterface() == StoreAndNextFrame) {

					edroomCurrentTrans.localId= StoreFrame;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



